#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <atomic>
#include <fstream>
#include <vector>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    class Timeutil
    {
    public:
        Timeutil() {}
        ~Timeutil() {}

        // 获取时间戳
        static std::string GetTimeStamp()
        {
            struct timeval time;
            gettimeofday(&time, nullptr);
            struct tm *tm_info = localtime(&time.tv_sec);
            char buffer[20]; // 格式为 YYYY-MM-DD HH:MM:SS
            strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info);
            return buffer;
        }

        static std::string GetTimeMS()
        {
            struct timeval time;
            gettimeofday(&time, nullptr);
            return std::to_string(time.tv_usec * 1000 + time.tv_sec / 1000);
        }
    };

    const std::string tempPath = "./temp/";
    // 构建文件路径
    class Pathutil
    {
    public:
        static std::string Src(const std::string &file_name)
        {
            return FormFile(file_name, ".cpp");
        }

        static std::string Exe(const std::string &file_name)
        {
            return FormFile(file_name, ".exe");
        }

        static std::string Err(const std::string &file_name)
        {
            return FormFile(file_name, ".compile_error");
        }

        static std::string Stdin(const std::string &file_name)
        {
            return FormFile(file_name, ".stdin");
        }

        static std::string Stdout(const std::string &file_name)
        {
            return FormFile(file_name, ".stdout");
        }

        static std::string Stderr(const std::string &file_name)
        {
            return FormFile(file_name, ".stderr");
        }

        static std::string FormFile(const std::string &file_name, std::string tail)
        {
            return tempPath + file_name + tail;
        }
    };

    class Fileutil
    {
    public:
        Fileutil() {}
        ~Fileutil() {}
        static bool isExist(const std::string &file_name)
        {
            struct stat statbuf;
            if (stat(file_name.c_str(), &statbuf) == 0)
            {
                return true;
            }
            return false;
        }

        static std::string Makefilename()
        {
            std::string _time = Timeutil::GetTimeMS();
            static std::atomic<int> count(0);
            std::string file_name = _time + std::to_string(count);
            count++;
            return file_name;
        }

        static bool WriteFile(const std::string &file_name, const std::string &code)
        {
            std::ofstream out(file_name);
            if (!out.is_open())
            {
                return false;
            }
            out.write(code.c_str(), code.size());
            return true;
        }

        static bool ReadFile(const std::string &file_name, std::string *out, bool keep)
        {
            (*out).clear();
            std::ifstream in(file_name);
            if (!in.is_open())
            {
                return false;
            }
            // getline不会保存分割符需要手动添加
            std::string line;
            while (std::getline(in, line))
            {
                *out += line;
                (*out) += (keep) ? "\n" : "";
            }
            return true;
        }
    };

    class StringUtil
    {
    public:
        StringUtil() {}
        ~StringUtil() {}
        static void SplitString(std::vector<std::string> *tokens, const std::string &str, std::string sep)
        {
            boost::split((*tokens), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}
